﻿using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace xiushen.Tests
{
    [TestClass]
    public class TestSpiding
    {
      
        [TestMethod]
        public void NewTest5()
        {
          //  Spider.Initialize();
            var content = Spider.getHTML(new Uri("http://guji.artx.cn/article/7168.html"));
            string content_pattern = @"(?<=r_zhengwen"">)[\w\W]+(?=<div class=l_mulu_table>)";

            var regex = @"<font[^>]*?>(\w|\W)*?</font>";
            var regex2 = @"(<a[^>]*?>(\w|\W)*?</a>)|<(?!br|strong)[^>]*>";



            var line = Regex.Match(content, content_pattern).Value;
            var contentdetail = Regex.Replace(line, regex, string.Empty);
            contentdetail = Regex.Replace(line, regex2, string.Empty);


            Console.Write(contentdetail);

        }
        [TestMethod]
        public void NewTest6()
        {
            Spider.Initialize();
            var content = Spider.getHTML(new Uri("http://guji.artx.cn/article/703.html"));
            string content_pattern = @"(?<=r_zhengwen"">)[\w\W]+(?=<table class=gs_bj_block>)";

            var regex = @"<font[^>]*?>(\w|\W)*?</font>";
            var regex2 = @"(<a[^>]*?>(\w|\W)*?</a>)|<(?!br|strong)[^>]*>";



            var line = Regex.Match(content, content_pattern).Value;
            var contentdetail = Regex.Replace(line, regex, string.Empty);
            contentdetail = Regex.Replace(line, regex2, string.Empty);


            Console.Write(contentdetail);

        }
        [TestMethod]
        public void GetAuthoer()
        {
            var line =
                "<div class=\"dright_show_title_foot\">[ <a href=http://www.artx.cn/renwu/search.asp?searchtxt=陈寿 target=_blank>陈寿</a> <a href=http://www.artx.cn/renwu/search.asp?searchtxt=裴松之 target=_blank>裴松之</a>  ]";
            string pattern_Href = @"(?<=<a href=http://www.artx.cn/renwu/search.asp[^>]*?>)[^<>]+(?=</a>)";

            var booktitle = Regex.Matches(line, pattern_Href);
            Console.Write(booktitle[0].Value);
            Console.Write(booktitle[1].Value);

        }

        [TestMethod]
        public void NewTest4()
        {
            var line =
                "<font color=\"#0066CC\"><strong>魏略曰：太祖不时立太子，<strong/>太子自疑。<tr>是时有高元吕者，善相人，<br>乃呼问之，对曰：“其贵乃不可言。”问：“寿几何？”元吕曰：“其寿，至四十当有小苦，过是无忧也。”后无几而立为王太子，至年四十而薨。<div></font>";
            var regex = @"<font class=bj_style[^>]*?>(\w|\W)*?</font>";
            var regex2 = @"(<a[^>]*?>(\w|\W)*?</a>)|<(?!br|strong)[^>]*>";

            var booktitle = Regex.Replace(line, regex, string.Empty);
            booktitle = Regex.Replace(booktitle, regex2, string.Empty);
            Console.Write(booktitle);

        }
        [TestMethod]
        public void NewTest3()
        {
            var line = "<div class=\"dg_title\"><p color=\"#990000\"><b>经部</b> |";
            var regex = "(?<=<b>)[^<>]+(?=</b>)";
            var booktitle = Regex.Match(line, regex);
            Console.WriteLine(booktitle);

        }

        [TestMethod]
        public void NewTest2()
        {
            Spider.Initialize();
            string regexstr = @"<[^>]*>";    //去除所有的标签

            //@"<script[^>]*?>.*?</script>" //去除所有脚本，中间部分也删除

            // string regexstr = @"<img[^>]*>";   //去除图片的正则

            // string regexstr = @"<(?!br).*?>";   //去除所有标签，只剩br

            // string regexstr = @"<table[^>]*?>.*?</table>";   //去除table里面的所有内容

           // string regexstr = @"<(?!img|br|p|/p).*?>";   //去除所有标签，只剩img,br,p
            var result = Spider.getHTML(new Uri("http://guji.artx.cn/article/1024.html"));
         //   var booktitle = Regex.Match(result, regexstr);

          //  Console.WriteLine(booktitle);
            Console.WriteLine(result);
        }

        [TestMethod]
        public void NewTest()
        {
            var url = new Uri("http://ns.foyuan.net/book/Article_023_002.html");
            var content = Spider.getHTML(url);
            string content_pattern = @"(?<=<div id=""content"">)[^ul]+(?=</div>)";
            var booktitle = Regex.Match(content, content_pattern);

            Console.WriteLine(booktitle);
        }
        [TestMethod]
        public void Test()
        {
            var line = "<a title=\"阅读南怀瑾 《金刚经说什么》 序 超越宗教的大智慧\" href=\"Article_023_001.html\">《金刚经说什么》 序 超越宗教的大智慧</a>";
            var index = line.IndexOf("href=");
            var firstIndex = line.IndexOf("\"", index);
            var nextIndex = line.IndexOf("\">");
            var href = line.Substring(firstIndex + 1, nextIndex - firstIndex - 1);
            Console.WriteLine(href);
        }

        [TestMethod]
        public void Test2()
        {
            var line = "<a title=\"阅读南怀瑾 《金刚经说什么》 序 超越宗教的大智慧\" href=\"Article_023_001.html\">《金刚经说什么》 序 超越宗教的大智慧</a>";

            var match = new MatchStructure() { line = line, fisrtMath = "href=", secondMatch = "\"", lastMatch = "\">" };
            var result = MatchData(match);
            var host = string.Format("http://ns.foyuan.net/book/{0}", result);
            Console.WriteLine(result);

            match.fisrtMath = "href";
            match.secondMatch = ">";
            match.lastMatch = "</a>";
            var title = MatchData(match);
            Console.WriteLine(title);
        }

        [TestMethod]
        public void Test3()
        {
            var line = "<li><a title=\"阅读南怀瑾 《金刚经说什么》 序 超越宗教的大智慧\" href=\"Article_023_001.html\">《金刚经说什么》 序 超越宗教的大智慧</a></li>";
            string pattern_Href = @"(?<=<a[^<>]+href=""?)[^""<>]+(?=""?[^<>]*>)";
            string title = @"(?<=<a[^<>]+>?)[^""<>]+(?=[</a>])";
            string h1title = @"(?<=<h1>)[\s\S]+(?=</h1>)";

            MatchCollection col = Regex.Matches(line, title, RegexOptions.IgnoreCase);

            StringBuilder sb = new StringBuilder();
            // Añadimos al stringbuilder cada uno de los resultados
            foreach (Match match in col)
            {
                sb.Append(match.Value);
            }
            Console.WriteLine(sb.ToString());

        }
        [TestMethod]
        public void Test4()
        {
            var line = "<a title=\"阅读南怀瑾 《金刚经说什么》 序 超越宗教的大智慧\" href=\"Article_023_001.html\">《金刚经说什么》 序 超越宗教的大智慧</a>";
            string pattern_Href = @"(?<=<ul>)[\s\S]+(?=</ul>)";
            //  string title = @"(?<=<a[^<>]+title=""?)[^""<>]+(?=""?[^<>]*>)";
            string title = @"(?<=/>)[^""<>]+(?=<)";

            MatchCollection col = Regex.Matches(line, pattern_Href, RegexOptions.IgnoreCase);

            StringBuilder sb = new StringBuilder();
            // Añadimos al stringbuilder cada uno de los resultados
            foreach (Match match in col)
            {
                sb.Append(match.Value);
                sb.Append("<br />");
            }
            Console.WriteLine(sb.ToString());

        }
        [TestMethod]
        public void Test5()
        {
            var url = new Uri("http://ns.foyuan.net/book/List_023.html");
            var content = Spider.getHTML(url);
            string content_Href = @"(?<=<ul>)[\s\S]+(?=</ul>)";
            string pattern_Href = @"(?<=<a[^<>]+href=""?)[^""<>]+(?=""?[^<>]*>)";

            //  string title = @"(?<=<a[^<>]+title=""?)[^""<>]+(?=""?[^<>]*>)";
            string title_pattern = @"(?<=<a[^<>]+/>?)[^""<>]+(?=[</a>])";
            string booktitle_pattern = @"(?<=<h1>)[\s\S]+(?=</h1>)";
            var booktitle = Regex.Match(content, booktitle_pattern);

            Console.Write(booktitle);
            var col = Regex.Match(content, content_Href);
            var values = col.Value.Split(new[] { "</li>" }, StringSplitOptions.None);
            StringBuilder sb = new StringBuilder();
            // Añadimos al stringbuilder cada uno de los resultados
            foreach (var match in values)
            {
                var href = Regex.Match(match, pattern_Href);
                var host = string.Format("http://ns.foyuan.net/book/{0}", href);
                var title = Regex.Match(match, title_pattern);
                Console.WriteLine(title);

            }
            Console.WriteLine(sb.ToString());

        }
        [TestMethod]
        public void Test6()
        {
            	
            var line = @"</font><a href=""zl_004.htm"" target=""_self""><font color=""#18418C""> 夏官司马第四</font></a><font color=""#010E69""> ";
            var line2 = @"<a href=""http://guji.artx.cn/list_3_0_1.html"" target=""_blank"">正史</a> |";
            string pattern_Href = @"(?<=<a[^<>]+href=""?)[^""<>]+(?=""?[^<>]*>)";
            string title_pattern = @"(?<=<a+[^<>]+>)[^""<>]+(?=[</a>])";

           // string title_pattern = @"(?<=<a[\s\S]>)[\s\S]";
            string booktitle_pattern = @"(?<=<h1>)[\s\S]+(?=</h1>)";
            string content_pattern = @"(?<=<div id=""content"">)[^ul]+(?=</div>)";

         //   var col = Regex.Match(line, pattern_Href);
            var col2 = Regex.Match(line2, title_pattern);


           // Console.WriteLine(col.Value);
            Console.WriteLine(col2.Value);

        }
        [TestMethod]
        public void BookList()
        {
            var url = new Uri("http://ns.foyuan.net/book/index.html");
            var content = Spider.getHTML3(url);
            string content_Href = @"(?<=<td class=""bookname"")[\s\S]+(?=</a></td>)";
            string pattern_Href = @"(?<=<a[^<>]+href=""?)[^""<>]+(?=""?[^<>]*>)";

            //  string title = @"(?<=<a[^<>]+title=""?)[^""<>]+(?=""?[^<>]*>)";
            string title_pattern = @"(?<=<a[^<>]+/>?)[^""<>]+(?=[</a>])";
            string bookDescription = @"(?<=<td class=""indextdintr"">)[\s\S]+(?=</td>)";

            StreamReader reader = new StreamReader(new MemoryStream(content), Encoding.Default);

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                if (line.Contains(@"td class=""bookname"""))
                {
                    var href = Regex.Match(line, pattern_Href);
                    Console.WriteLine(href);
                }
                if (line.Contains(@"td class=""indextdintr"""))
                {
                    var description = Regex.Match(line, bookDescription);
                    Console.WriteLine(description);
                }

            }


        }
        [TestMethod]
        public void Test9()
        {
            var downLoad = new GuJiDownLoad();
            downLoad.DownLoadAllBookList("http://guji.artx.cn/list_2_0_1.html", "正史");

            foreach (var d in downLoad.bookList)
            {
                Console.WriteLine(d.Key);
                foreach (var book in d.Value)
                {
                    Console.WriteLine(book.Name);
                //    Console.WriteLine(book.URL);
                }
            }
        }
        [TestMethod]
        public void Test10()
        {
            var downLoad = new GuJiDownLoad();
          //  downLoad.DownLoadBookChapters("http://guji.artx.cn/Article/704.html", "正史");

            foreach (var d in downLoad.chapterList)
            {
                Console.WriteLine(d.Key);
                foreach (var book in d.Value)
                {
                    Console.WriteLine(book.Name);
                  //  Console.WriteLine(book.URL);
                }
            }
        }
        [TestMethod]
        public void Test8()
        {
            var downLoad = new GuJiDownLoad();
            downLoad.DownLoadBookList("http://guji.artx.cn/");

        }
        [TestMethod]
        public void Test11()
        {
            var downLoad = new GuJiDownLoad();
            downLoad.DownLoadAllBookList("http://guji.artx.cn/list_4_8_1.html", "医家");

        }
        private string MatchData(MatchStructure m)
        {
            var line = m.line;
            var index = line.IndexOf(m.fisrtMath);
            var firstIndex = line.IndexOf(m.secondMatch, index);
            var nextIndex = line.IndexOf(m.lastMatch);
            return line.Substring(firstIndex + 1, nextIndex - firstIndex - 1);
        }
    }
}